package com.manpower.base;


import com.manpower.common.BusinessException;
import com.manpower.common.ResponseStatus;
import com.manpower.util.RedisUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 身份认证拦截
 * @ClassName AuthInterceptor
 * @Author weiyong.wang
 * @Date 2020/8/17
 */
@Component
public class AuthInterceptor implements HandlerInterceptor {

	private static Logger logger = LoggerFactory.getLogger(AuthInterceptor.class);

	@Autowired
	private RedisUtil redisUtil;

	@Autowired
	private Environment env;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		String method = request.getMethod();
		
		if("OPTIONS".equals(method)) {//如果OPTIONS说明是跨域认证，直接返回即可
			response.setStatus(200);
			return false;
		}

		//String accessToken = redisUtil.get("accessToken", String.class);
		/*String accessToken = null;
		if (StringUtils.isEmpty(accessToken)) {
			response.setStatus(200);
			throw new BusinessException(ResponseStatus.UNREGISTERED.value(), "登录凭证为空，请重新登陆！");
		} else {
			// 设置失效时间
			redisUtil.put("accessToken",accessToken,Integer.parseInt(env.getProperty("sys.Timeout")));
		}*/
		System.out.println("》》》身份验证通过》》》》》》》》》》》》》");
		return true;
	}

}
